#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
# Author: Adam Grycner (adam_gr [at] gazeta.pl)
#
# Written: 08/09/2010
#
# Released under: GNU GENERAL PUBLIC LICENSE
#
# Ver: 0.1



import mechanize
import socket
import random
import urllib2
import re

class Creator(object):

    def __init__(self):
        socket.setdefaulttimeout(240)
        self.reset()
        
    def reset(self, proxy = None):
        self.br                = mechanize.Browser()
        self.br.addheaders = [('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')]
        self.br.set_handle_robots(False)
        #random proxy
        f           = open('proxy.txt','r')
        proxies     = f.readlines()
        f.close()
        proxy =  random.sample(proxies,1)[0].strip()
        if proxy != None:
            print proxy
            self.br.set_proxies( {'http': '127.0.0.1:2080'} )
    
    def create(self, mail, name, paswd = 'haslo1234', proxy = None):
        resp = self.br.open('https://www.alwaysdata.com/subscribe/1/')
        #resp = self.br.open('http://www.whatismyip.com/')
        #print resp.read()
        #resp.close()
        #return
        self.br.select_form(nr = 0)
        self.br["pack"] = ["1"]
        self.br["name"] = name
        self.br.submit()
        self.br.select_form(nr = 0)
        self.br["email"]     = mail
        self.br["password"]  = paswd
        self.br["password2"] = paswd
        self.br.submit()
        resp = self.br.open("https://www.alwaysdata.com/subscribe/4/?")
        #print resp.read()
        resp.close()

    
    def activate_ssh(self, mail, paswd):
        self.br.open('https://admin.alwaysdata.com/ssh/')
        self.br.select_form(nr = 0)
        self.br["username"] = mail
        self.br["password"] = paswd
        self.br["alive"]    = ["on"]
        resp = self.br.submit()
        cont = resp.read()
        resp.close()
        num = re.findall( """title=\"modifier\" href=\"/ssh/(\d*)""", cont)[0]
        self.br.open('https://admin.alwaysdata.com/ssh/' + num + "/")
        self.br.select_form(nr = 1)
        self.br["is_enabled"]    = ["on"]
        self.br.submit()
        


        
if __name__ == '__main__':
    #c = ChomikCreator()
    #c.creator()
    for mail in ["wanemen@wp.pl", "crookeshanks@wp.pl", "pekuar@wp.pl", "seriph89@wp.pl", "ethkan@wp.pl", "aleicht@wp.pl", "infernus58@wp.pl", "alfric@wp.pl", "varos0@wp.pl", "shadowthorn29@wp.pl", "seriph@wp.pl", "vinuh@wp.pl", "nudgestone@wp.pl", "zindlestone@wp.pl", "knacknell@wp.pl", "nordobo@wp.pl"]:
      while True:
        try:
          print mail
          user, _, _ = mail.partition("@")
          c = Creator()
          print "Creating"
          c.create(mail, user, "haslo1234")
          print "Activating"
          c.activate_ssh(mail, "haslo1234")
        except socket.error, e:
          pass
        else:
          break
